package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        Map<String,Object> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        Integer totalOrders = ordersMapper.countByMap(map);
        totalOrders = totalOrders == null ? 0 : totalOrders;

        map.put("status", Orders.COMPLETED);

        Double turnover = ordersMapper.sumByMap(map);//营业额
        turnover = turnover == null ? 0.0 : turnover;

        Integer validOrderCount = ordersMapper.countByMap(map);//有效订单数
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;

        Double orderCompletionRate = totalOrders == 0 ? 0.0 : (validOrderCount * 1.0) / (totalOrders * 1.0);//订单完成率

        Double unitPrice = validOrderCount == 0 ? 0.0 :(turnover / validOrderCount);//平均客单价

        Integer newUsers = userMapper.countByMap(map);
        newUsers = newUsers == null ? 0 : newUsers;

        return BusinessDataVO
                .builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(Math.round(unitPrice * 100.0) / 100.0)
                .newUsers(newUsers)
                .build();
    }

    @Override
    public DishOverViewVO getOverviewDishes() {
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        return DishOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(),LocalTime.MAX);
        Map<String,Object> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        //待接单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = ordersMapper.countByMap(map);

        //待派送数量
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = ordersMapper.countByMap(map);

        //已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = ordersMapper.countByMap(map);

        //已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = ordersMapper.countByMap(map);

        //全部订单
        map.put("status",null);
        Integer allOrders = ordersMapper.countByMap(map);

        return OrderOverViewVO
                .builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }
}
